<?php 

/**
 * 
 * @author Kartik
 *
 *	Stopped working at orderdetails
 */

class Packing_Resource_USPS extends Packing_Resource_Shippinglabel{

	const SERVER_TYPE_ERROR = "Incorrect UPSP Server selected";
	
	//XML String For Parsing.
	private $xmlResponse;
	
	//Static Variables
   private $uri = "https://secure.shippingapis.com/ShippingAPI.dll";
   private $api;
   private $root; //root element of the xml data sent to USPS
   
   //Retailer userid and password
   private $userid  = "292MYBOO6721";
   private $password = "568RV79VT640";
   
   //Service type Information should remain unchanged hence assigned permanently
   private $Option = "RIGHTWINDOW";
   private $ServiceType = "Media Mail";
   private $DeliveryConfirmation = "True";
   private $Insurance = "";

   //ImageType
   private $ImageType = "PDF";   //valid values are PDF and TIF, even though GIF would be the best

   /* -------- Dynamic Variables to be set at RUN TIME ----------- */

   //Retailer Information
   /** Retailer array contains
      1. RetailerName
      2. RetailerAddress
   **/
   private $Retailer = array();

   //Permit Information
   /** Permit array contains
      1. PermitNumber
      2. PermitIssuingPOCity
      3. PermitIssuingPOState;
      4. PermitIssuingPOZip5;
   **/
   private $Permit = array();

   //Postal Delivery Unit Information
   /** Postal Delivery Unit array contains
      1. PDUPOBox
      2. PDUCity
      3. PDUState
      4. PDUZip5
      5. PDUZip4
   **/
   private $PDU = array();
   
   public function __construct($serverType = "test"){

   	parent::__construct();
   	
      if($serverType === "test"){

      	         //Selecting the root level element
         $this->root = "EMRSV4.0CertifyRequest";

         //Selecting the API type
         $this->api = "MerchReturnCertifyV4";

         //Customer Information
         $this->Customer = array(
            //Details needed for the USPS api
            "CustomerName"       =>    "Garrison Johns",
            "CustomerAddress2"   =>    "6406 Ivy Lane",
            "CustomerAddress1"   =>    "TEST 40", //It would appear the US Postal Service has a sense of humor
            "CustomerCity"       =>    "Greenbelt",
            "CustomerState"      =>    "MD",
            "CustomerZip5"       =>    "20770",
            "CustomerZip4"       =>    "",
         );

         //Retailer Information
         $this->Retailer = array(
            "RetailerName" => "Reza Dianat",
            "RetailerAddress" => "6406 Ivy Lane",
         );

         //Permit Information
         $this->Permit = array(
            "PermitNumber" => "293829",
            "PermitIssuingPOCity" => "Greenbelt",
            "PermitIssuingPOState" => "MD",
            "PermitIssuingPOZip5" => "20770",
         );

         //Postage Delivery Unit Information
         $this->PDU = array(
            "PDUPOBox" => "6406 Ivy Lane",
            "PDUCity" => "Geenbelt",
            "PDUState" => "MD",
            "PDUZip5" => "20770",
            "PDUZip4" => "1234",
         );

         //$this->MackPackID = "ID00001";

         $this->RMA = array(
            "RMA" => "RMA 123456",
            "RMAPICFlag" => "False",
            "RMABarcode" => "False",
         );

         //Temposrary weight Parameters
         $this->Weight = array(
            "Pounds" => 0,
            "Ounces" => 10,
         );
      } else if($serverType === "production"){

         //Selecting the root level element
         $this->root = "EMRSV4.0CertifyRequest";

         //Selecting the API type
         $this->api = "MerchReturnCertifyV4";

         //Retailer Information
         $this->Retailer = array(
            "RetailerName" => "Beagle Books",
            "RetailerAddress" => "1006 S Milpitas Blvd",
         );

         //Permit Information
         $this->Permit = array(
            "PermitNumber" => "10",
            "PermitIssuingPOCity" => "Milpitas",
            "PermitIssuingPOState" => "CA",
            "PermitIssuingPOZip5" => "95035",
         );

         //Postage Delivery Unit Information
         $this->PDU = array(
            "PDUPOBox" => "450 S Abel St",
            "PDUCity" => "Milpitas",
            "PDUState" => "CA",
            "PDUZip5" => "95035",
            "PDUZip4" => "9998",
         );

         $this->RMA = array(
            "RMA" => "",
            "RMAPICFlag" => "False",
            "RMABarcode" => "False",
         );

      } else if($serverType === "live"){

         //Selecting the root level element
         $this->root = "EMRSV4.0Request";

         //Selecting the API type
         $this->api = "MerchandiseReturnV4";

         //Retailer Information
         $this->Retailer = array(
            "RetailerName" => "Beagle Books",
            "RetailerAddress" => "1006 S Milpitas Blvd",
         );

         //Permit Information
         $this->Permit = array(
            "PermitNumber" => "10",
            "PermitIssuingPOCity" => "Milpitas",
            "PermitIssuingPOState" => "CA",
            "PermitIssuingPOZip5" => "95035",
         );

         //Postage Delivery Unit Information
         $this->PDU = array(
            "PDUPOBox" => "450 S Abel St",
            "PDUCity" => "Milpitas",
            "PDUState" => "CA",
            "PDUZip5" => "95035",
            "PDUZip4" => "9998",
         );

         $this->RMA = array(
            "RMA" => "",
            "RMAPICFlag" => "False",
            "RMABarcode" => "False",
         );
      } else {
         throw new Exception(self::SERVER_TYPE_ERROR);
      }
   }

   public function createXML(){

      //Create XML Document without the xml tag
      $str = "<" . $this->root . " USERID=\"" . $this->userid . "\" PASSWORD=\"" . $this->password . "\">";
      $str .= "<Option>" . $this->Option . "</Option>";

      //Customer Details
      $str .= "<CustomerName>" . $this->Customer["CustomerName"] . "</CustomerName>";
      $str .= "<CustomerAddress1>" . $this->Customer["CustomerAddress1"] . "</CustomerAddress1>";
      $str .= "<CustomerAddress2>" . $this->Customer["CustomerAddress2"] . "</CustomerAddress2>";
      $str .= "<CustomerCity>" . $this->Customer["CustomerCity"] . "</CustomerCity>";
      $str .= "<CustomerState>" . $this->Customer["CustomerState"] . "</CustomerState>";
      $str .= "<CustomerZip5>" . $this->Customer["CustomerZip5"] . "</CustomerZip5>";
      if(empty($this->Customer["CustomerZip4"]))
         $str .= "<CustomerZip4/>";
      else
         $str .= "<CustomerZip4>" . $this->Customer["CustomerZip4"] . "</CustomerZip4>";

      //Retailer Details
      $str .= "<RetailerName>" . $this->Retailer["RetailerName"] . "</RetailerName>";
      $str .= "<RetailerAddress>" . $this->Retailer["RetailerAddress"] . "</RetailerAddress>";

      //Permit Details
      $str .= "<PermitNumber>" . $this->Permit["PermitNumber"] . "</PermitNumber>";
      $str .= "<PermitIssuingPOCity>" . $this->Permit["PermitIssuingPOCity"] . "</PermitIssuingPOCity>";
      $str .= "<PermitIssuingPOState>" . $this->Permit["PermitIssuingPOState"] . "</PermitIssuingPOState>";
      $str .= "<PermitIssuingPOZip5>" . $this->Permit["PermitIssuingPOZip5"] . "</PermitIssuingPOZip5>";

      //Postal Delivery Unit
      $str .= "<PDUPOBox>" . $this->PDU["PDUPOBox"] . "</PDUPOBox>";
      $str .= "<PDUCity>" . $this->PDU["PDUCity"] . "</PDUCity>";
      $str .= "<PDUState>" . $this->PDU["PDUState"] . "</PDUState>";
      $str .= "<PDUZip5>" . $this->PDU["PDUZip5"] . "</PDUZip5>";
      $str .= "<PDUZip4>" . $this->PDU["PDUZip4"] . "</PDUZip4>";

      //Service Type
      $str .= "<ServiceType>" . $this->ServiceType . "</ServiceType>";

      //Delivery Confirmation
      $str .= "<DeliveryConfirmation>" . $this->DeliveryConfirmation . "</DeliveryConfirmation>";

      //Insurance Value
      if(empty($this->Insurance))
         $str .= "<InsuranceValue/>";
      else
         $str .= "<InsuranceValue>" . $this->Insurance . "</InsuranceValue>";

      //Mailing Acknowledge Package ID
      //$str .= "<MailingAckPackageID>" . $this->MackPackID . "</MailingAckPackageID>";

      //Package Weight In Pounds
      if($this->getWeightInPounds() <= 30){
         $str .= "<WeightInPounds>" . $this->getWeightInPounds() . "</WeightInPounds>";
      }else {
         $str .= "<WeightInPounds>30</WeightInPounds>";
      }
      //Package Weight In Ounces
      if($this->getWeightInOunces() <= 480){
         $str .= "<WeightInOunces>" . $this->getWeightInOunces() . "</WeightInOunces>";
      }else {
         $str .= "<WeightInOunces>480</WeightInOunces>";
      }

      //RMA
      $str .= "<RMA>" . $this->RMA["RMA"] . "</RMA>";
      $str .= "<RMAPICFlag>" . $this->RMA["RMAPICFlag"] . "</RMAPICFlag>";

      //Image Type Definition
      $str .= "<ImageType>" . $this->ImageType . "</ImageType>";

      //RMABarcode Information
      $str .= "<RMABarcode>" . $this->RMA["RMABarcode"] . "</RMABarcode>";

      //Random Definition dont know what it means but I am putting it there just in case since all the examples have it.
      $str .= "<AllowNonCleansedDestAddr>False</AllowNonCleansedDestAddr>";

      //Closing Tag
      $str .= "</" . $this->root . ">";

      return $str;
   }

	public function cURLCall(){

		$url = $this->uri . "?API=" . $this->api . "&XML=" . rawurlencode($this->createXML());

		if(function_exists("curl_init")){
			$ch = curl_init();
			// set URL and other appropriate options
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			// grab URL and pass it to the browser
			$this->xmlResponse = curl_exec($ch);
			return $this->xmlResponse;
		}else {
			return "cURL not found on server";
		}
	}

	public function ParseXML(){
		$this->xmlResponse = new SimpleXMLElement($this->xmlResponse);
		return $this->xmlResponse;
	}

	public function checkError(){
		return $this->xmlResponse->Description;
	}

	public function getPackingSlip(){
		return $this->xmlResponse->MerchandiseReturnLabel;
	}

	public function getTrackingNumber(){
		return $this->xmlResponse->DeliveryConfirmationNumber;
	}

	/*public function getDeliveryConfirmationNumber(){
		return $this->xmlResponse->DeliveryConfirmationNumber;
	}*/

	public function getCustomerAddressFromUSPS(){
		//Customer Address1
		$this->Customer["CustomerAddress1"] = trim($this->xmlResponse->CustomerAddress1);
		$this->Customer["CustomerAddress2"] = trim($this->xmlResponse->CustomerAddress2);
		//Customer City
		$this->Customer["CustomerCity"] = $this->xmlResponse->CustomerCity;
		//Customer State
		$this->Customer["CustomerState"] = $this->xmlResponse->CustomerState;
		//Customer Zip5
		$this->Customer["CustomerZip5"] = $this->xmlResponse->CustomerZip5;
		//Customer Zip4
		$this->Customer["CustomerZip4"] = $this->xmlResponse->CustomerZip4;

		return $this->Customer;
	}
}